#ifndef __LOCK_H__
#define __LOCK_H__

#include "common.h"
#include <pthread.h>

typedef enum{
	READ_LOCK,
	WRITE_LOCK,
} LockFlag;

template< typename LOCKTYPE >
class LockBase{
public:
	LockBase( LOCKTYPE& rlock ):mrLock(rlock){
	}
	virtual ~LockBase(){
	}
	virtual bool TryLock() = 0;
	virtual void Lock() = 0;
	virtual void Unlock() = 0 ;
	virtual bool ChangeLock( LockFlag flag) = 0;
	virtual bool IsLocked() const{
		return mLocked;
	}
protected:
	LOCKTYPE& mrLock;
	bool mLocked;
};

template< typename T>
class LockFree:public LockBase<T>{
public:
	LockFree( T& lock, bool lockNow=true ):LockBase<T>(lock){
		this->mLocked = lockNow;
	}

	bool TryLock(){
		this->mLocked = true;
		return true;
	} 
	void Lock(){
		this->mLocked = true;
		return;
	}
	void Unlock(){
		this->mLocked = false;
		return;
	}
	bool ChangeLock( LockFlag flag){
		flag = flag;
		return false;
	}
};
#endif

